package com.wframe.project.jnd.services.impl;

import com.wframe.project.jnd.dao.UiFloorDao;
import com.wframe.project.jnd.entity.UiFloor;
import com.wframe.project.jnd.entity.UiFloorItem;
import com.wframe.project.jnd.entity.UiFloorPanel;
import com.wframe.project.jnd.entity.UiFloorProduct;
import com.wframe.project.jnd.services.UiFloorService;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.SerializationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.persistence.Column;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/5/24.
 */
@Service
public class UiFloorServiceImpl implements UiFloorService {

    @Autowired
    UiFloorDao uiFloorDao;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    private static final String FLOOR_BY_PLATFORM_KEY = "PLATFORM_FLOORs_DATA_CACHE_";

    @Override
    public List<UiFloor> getFloorByPlatform(UiFloor.Platform platform, Long userId) {
        ArrayList<UiFloor> floors = null;
        if (platform == null) {
            return floors;
        }

        try {

            //Gson gson = new Gson();
            String cacheKey = FLOOR_BY_PLATFORM_KEY + platform.toString();
            if (this.stringRedisTemplate != null && this.stringRedisTemplate.opsForValue().get(cacheKey) != null) {
                String data = this.stringRedisTemplate.opsForValue().get(cacheKey).trim();
//                floors = gson.fromJson(jsonData, List.class);
                floors = SerializationUtils.deserialize(Base64.decodeBase64(data));
            } else {
                floors = this.uiFloorDao.getAvailableFloors(platform);
//                String data = gson.toJson(floors);
                String data = Base64.encodeBase64String(SerializationUtils.serialize(floors));
                this.stringRedisTemplate.opsForValue().set(cacheKey, data, 10, TimeUnit.MINUTES);
            }

            if (userId != null && floors != null && floors.size() > 0) {
                Iterator<UiFloor> uiFloorIterator = floors.iterator();
                while (uiFloorIterator.hasNext()) {
                    UiFloor uiFloor = uiFloorIterator.next();
                    if (uiFloor.getPanels() != null && uiFloor.getPanels().size() > 0) {
                        Iterator<UiFloorPanel> uiFloorPanelIterator = uiFloor.getPanels().iterator();
                        while (uiFloorPanelIterator.hasNext()) {
                            UiFloorPanel uiFloorPanel = uiFloorPanelIterator.next();
                            if (uiFloorPanel != null && uiFloorPanel.getId() != null && uiFloorPanel.getType() == UiFloorPanel.Type.PRODUCT) {
                                List<UiFloorProduct> uiFloorProducts = this.uiFloorDao.getUiFloorProducts(uiFloorPanel.getId(), userId);
                                uiFloorPanel.setProducts(uiFloorProducts);
                            }
                        }
                    }
                }
            }

        } catch (Exception e1) {
            e1.printStackTrace();
            try {
                floors = this.uiFloorDao.getAvailableFloors(platform);
            } catch (Exception e2) {
                floors = null;
            }
        }

        return floors;
    }

    @Override
    @Transactional
    public void deleteItems(Map map) {
        uiFloorDao.delete("ui_floor_item", map);
    }

    @Override
    @Transactional
    public void saveOrUpdate(UiFloorItem item) {
        try {
            uiFloorDao.saveOrUpdate("ui_floor_item", item);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    @Override
    @Transactional
    public void deleteProducts(Map map) {
        uiFloorDao.delete("ui_floor_product", map);
    }

    @Override
    @Transactional
    public void addProducts(List<UiFloorProduct> products) {
        try {
            UiFloorProduct product = products.get(0);
            Class clazz = product.getClass();
            Field id = clazz.getField("id");
            Column column = id.getAnnotation(Column.class);
            //删除商品
            uiFloorDao.delete("ui_floor_product", column.name(), product.getUi_floor_panel_id());
            uiFloorDao.saveOrUpdate("ui_floor_product", products);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    @Override
    public UiFloorItem findOne(String id) {
        return uiFloorDao.findOne(id);
    }

    @Override
    public List<UiFloorItem> findAll(Long id) {
        return uiFloorDao.getUiFloorItems(id);
    }


}
